<?php

namespace App\Http\Controllers\Wechat\Shop;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\ProductRepo;
use App\Http\Requests\Api\Shop\ProductRequest;

class ProductController extends Controller
{
    public function index(ProductRequest $request)
    {
    	$rs['status']     = false;
        $where['type_id'] = $request->type_id;
        $where['shop_id'] = $request->shopUser->shop_id;
        //$where['on_sale'] = 1;
        $num = object_get($request,'num',5);
        $fields = ['id','name','thumb','price','origin_price','weight','is_manjian','is_tejia','on_sale'];
        $list = ProductRepo::getListPages($where,$num,$fields);
        if($list) {
            $rs['status'] = true;
            $rs['data'] = $list;
        }
        return response()->json($rs)->setEncodingOptions(JSON_NUMERIC_CHECK);;  
    }

    /**
     * 详情
     */
    public function show(ProductRequest $request)
    {
        $rs['status'] = false;
    	$where['id'] = $request->product_id;
        $where['shop_id'] = $request->shopUser->shop_id;
        
        $data      = ProductRepo::find($where);       
        
        if($data) {
            $rs['status'] = true;
            $rs['data']  = $data;
            // 如果是拼购商品要显示拼购信息
            if($data->type_id==2) {
                $rs['data']['group'] = ProductRepo::group($request->product_id);
                           
            }
            $rs['data']['imgs']  = ProductRepo::imgs($request->product_id);
            
                    
           
        }
        return response()->json($rs);  
    }

    /**
     * 保存商品信息
     */
    public function save(ProductRequest $request)
    {
        $rs['status'] = false;
        $data         = $request->pure();

        $pics = array_pull($data,'pics');
        $tags = array_pull($data,'tags');
        $group = [];
        // 如果是团购商品
        if($request->type_id == 2) {
            $group = array_pull($data,'group');
        }
        //dd($data,$tags);
        $data['shop_id'] = $request->shopUser->shop_id;
        $data['code']    = orderCode('P',$request->shopUser->id);
        $flag = ProductRepo::create($data,$tags,$pics,$group);
        if($flag['status'] == false) {
            $rs['msg'] = $flag['msg'];
            return response()->json($rs);
        }
        $rs['status'] = true;
        return response()->json($rs);    
    }

    /**
     * 更新商品信息
     */
    public function update(ProductRequest $request)
    {
        $rs['status'] = false;
        $data         = $request->pure();
        $data['shop_id'] = $request->shopUser->shop_id;
        $pics = array_pull($data,'pics');
        $tags = array_pull($data,'tags');
        $group = [];
        // 如果是团购商品
        if($request->type_id == 2) {
            $group = array_pull($data,'group');
        }
        $id = array_pull($data,'product_id');        
        $flag = ProductRepo::update($id,$data,$tags,$pics,$group);
        if($flag['status'] == false) {
            $rs['msg'] = $flag['msg'];
            return response()->json($rs);
        }
        $rs['status'] = true;
        return response()->json($rs);   
    }
}
